﻿using DB.Dapper;
using DB.EF;
using EF.DB;
using Orther.Model.param;
using Project.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.ManageUser;
using VOL.Entity.DomainModels;
using static Dapper.SqlMapper;

namespace Orther.Service.Sys
{
    public class OperateLogService
    {
        public MysqlContext mysqlContext = new MysqlContext();
        private string mainSql = "select a.* from operatelog a left join sys_user b on a.CreateID= b.User_Id where 1=1 ";
        public List<operatelog> GetPageList(OperatelogParam param, Pagination pagination)
        {
            int total = 0;
            FilterClass filterClass = this.WhereFilter(param);
            List<operatelog> list = DapperUtilsForMysql.PaginationZjc<operatelog>(mainSql, pagination.PageIndex, pagination.PageSize, ref total, "CreateDate desc", filterClass.where, filterClass.parameters);
            pagination.TotalCount = total;
            return list;
        }

        public async Task<List<operatelog>> GetList(OperatelogParam param, int? topNum)
        {
            FilterClass filterClass = this.WhereFilter(param);
            List<operatelog> list = await DapperUtilsForMysql.queryList<operatelog>(mainSql, filterClass.parameters, "CreateDate desc", filterClass.where, topNum);
            return list;
        }

        private FilterClass WhereFilter(OperatelogParam param)
        {
            FilterClass filterClass = new FilterClass();
            /*
            if (param.Miyao_Id != null)
            {
                filterClass.where = filterClass.where + " and Miyao_Id=@Miyao_Id ";
                filterClass.parameters.Add("@Miyao_Id", param.Miyao_Id);
            }
            */
            if (param.deptIds != null && param.deptIds.Count > 0)
            {
                string insql = "";
                for (int i = 0; i < param.deptIds.Count; i++)
                {
                    if (i == 0)
                    {
                        insql = insql + " FIND_IN_SET('" + param.deptIds[0].ToString() + "', b.DeptIds) > 0 ";
                    }
                    else
                    {
                        insql = insql + " or FIND_IN_SET('" + param.deptIds[i].ToString() + "', b.DeptIds) > 0 ";
                    }

                }

                filterClass.where = filterClass.where + " and (" + insql + ") ";
            }


            if (!string.IsNullOrEmpty(param.Creator))
            {
                filterClass.where = filterClass.where + " and a.Creator like @Creator ";
                filterClass.parameters.Add("@Creator", '%' + param.Creator + '%');
            }

            return filterClass;
        }


        public bool SaveForm(operatelog oLog)
        {
            oLog.Log_Id = Guid.NewGuid();
            oLog.CreateDate = DateTime.Now;
            oLog.CreateID = UserContext.Current.UserId;
            oLog.Creator = UserContext.Current.UserName;
            
            try
            {
                Action myAction = () =>
                {
                    mysqlContext.Set<operatelog>().Add(oLog);
                    //最后添加SaveChanges
                    mysqlContext.SaveChanges();
                };
                var transactionWrapper = new TransactionWrapper(mysqlContext);
                transactionWrapper.UseTransaction(myAction);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
                return false;
            }
            

        }

    }
}
